//
//  UIViewController+YSBubbleTransition.m
//  YSKitDemo
//
//  Created by MOLBASE on 2018/8/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "UIViewController+YSBubbleTransition.h"
#import "YSBubbleTransition.h"
#import <objc/runtime.h>

@implementation UIViewController (YSBubbleTransition)


#pragma mark - setter & getter
- (void)setYs_pushTranstion:(YSBubbleTransition *)ys_pushTranstion {
    if (!ys_pushTranstion) {return;}
    ys_pushTranstion.transitionType = YSBubbleTransitionTypeShow;
    self.navigationController.delegate = self;
    objc_setAssociatedObject(self, @selector(ys_pushTranstion), ys_pushTranstion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YSBubbleTransition *)ys_pushTranstion {
    return objc_getAssociatedObject(self, @selector(ys_pushTranstion));
}

- (void)setYs_popTranstion:(YSBubbleTransition *)ys_popTranstion {
    if (!ys_popTranstion) {return;}
    ys_popTranstion.transitionType = YSBubbleTransitionTypeHide;
    self.navigationController.delegate = self;
    objc_setAssociatedObject(self, @selector(ys_popTranstion), ys_popTranstion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YSBubbleTransition *)ys_popTranstion {
    return objc_getAssociatedObject(self, @selector(ys_popTranstion));
}

- (void)setYs_presentTranstion:(YSBubbleTransition *)ys_presentTranstion {
    if (!ys_presentTranstion) {return;}
    ys_presentTranstion.transitionType = YSBubbleTransitionTypeShow;
    self.transitioningDelegate = self;
    objc_setAssociatedObject(self, @selector(ys_presentTranstion), ys_presentTranstion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YSBubbleTransition *)ys_presentTranstion {
    return objc_getAssociatedObject(self, @selector(ys_presentTranstion));
}

- (void)setYs_dismissTranstion:(YSBubbleTransition *)ys_dismissTranstion {
    if (!ys_dismissTranstion) {return;}
    ys_dismissTranstion.transitionType = YSBubbleTransitionTypeHide;
    self.transitioningDelegate = self;
    objc_setAssociatedObject(self, @selector(ys_dismissTranstion), ys_dismissTranstion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YSBubbleTransition *)ys_dismissTranstion {
    return objc_getAssociatedObject(self, @selector(ys_dismissTranstion));
}

#pragma mark - Navigation的Push和Pop转场动画设置
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush && [fromVC isEqual:self]) {
        return self.ys_pushTranstion;
    }else if(operation == UINavigationControllerOperationPop && [toVC isEqual:self]) {
        return self.ys_popTranstion;
    }else{
        return nil;
    }
}

#pragma mark - Present和Dismiss转场动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return (id<UIViewControllerAnimatedTransitioning>)self.ys_presentTranstion;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return (id<UIViewControllerAnimatedTransitioning>)self.ys_dismissTranstion;
}

@end
